package com.business.photo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.photo.PhotoReply;
import com.sql.DBFactory;

public class PhotoReplyBus {

	private Connection conn = null;

	private Statement stmt = null;

	private ResultSet rs = null;

	private static final Log log = LogFactory.getLog(PhotoReply.class);

	public void add(PhotoReply pr) {
		String nickname = pr.getNickname();
		String replyContent = pr.getReplyContent();
		String replyTime = pr.getReplyTime();
		String photoNo = pr.getPhotoNo();
		String sql = "insert into photo_reply(nickname,reply_content,reply_time,photo_no) values('";
		sql += nickname + "','" + replyContent + "','" + replyTime + "','"
				+ photoNo + "')";
		List list = getReplyByPhotoNo(photoNo);
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			sql = "update photo_detail set reply_count=" + (list.size() + 1)
					+ " where photo_no='" + photoNo + "'";
			// System.out.println(sql);
			stmt.executeUpdate(sql);

		} catch (Exception e) {
			log.error("the insert photoreply exception:" + e.getMessage());
			log.error("the insert photoreply sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	@SuppressWarnings("unchecked")
	public List getReplyByPhotoNo(String PhotoNo) {
		List list = new ArrayList();
		String sql = "select * from photo_reply where photo_no='" + PhotoNo
				+ "' order by reply_time desc";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				PhotoReply lr = new PhotoReply();
				lr.setPhotoNo(PhotoNo);
				lr.setNickname(rs.getString("nickname"));
				lr.setReplyContent(rs.getString("reply_content"));
				lr.setReplyId(rs.getString("reply_id"));
				lr.setReplyTime(rs.getString("reply_time"));
				list.add(lr);
			}
		} catch (Exception e) {
			log.error("the select photoreply exception:" + e.getMessage());
			log.error("the select photoreply sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

		return list;
	}

	public PhotoReply getReplyById(String replyId) {
		PhotoReply lr = new PhotoReply();
		String sql = "select * from photo_reply where reply_id=" + replyId;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				lr.setPhotoNo(rs.getString("photo_no"));
				lr.setNickname(rs.getString("nickname"));
				lr.setReplyContent(rs.getString("reply_content"));
				lr.setReplyId(rs.getString("reply_id"));
				lr.setReplyTime(rs.getString("reply_time"));
			}
		} catch (Exception e) {
			log.error("the select photoreply exception:" + e.getMessage());
			log.error("the select photoreply sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

		return lr;

	}

	public void del(String replyId) {
		String sql = "delete from photo_reply where reply_id=" + replyId;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update photoreply exception:" + e.getMessage());
			log.error("the update photoreply sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void update(PhotoReply pr, String replyId) {
		String replyContent = pr.getReplyContent();
		String replyTime = pr.getReplyTime();
		String nickname = pr.getNickname();
		String photoNo = pr.getPhotoNo();
		String sql = "update photo_reply set reply_content='" + replyContent
				+ "',reply_time='" + replyTime + "',nickname='" + nickname
				+ "',photo_no='" + photoNo + "'  where reply_id=" + replyId;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update photoreply exception:" + e.getMessage());
			log.error("the update photoreply sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}
}
